/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ventasimple.dao;

import com.ventasimple.modelo.Cliente;
import com.ventasimple.modelo.Publicacion;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * @author Claudio
 */
public class ClienteDAO extends BaseDAO<Cliente> implements Serializable {
    
    public Cliente getClienteByLink(String link) {
         
        EntityManager em = null;
        String sQuery = "";
        try {
           sQuery = "SELECT c FROM Cliente c "
                +" WHERE c.activo = 'S' "
                +" AND p.link =:link ";                                       
                        
           em = getEntityManager();           
           Query q = em.createQuery(sQuery);
           
           q.setParameter("link", link);
           
          
           return (Cliente) q.getSingleResult();
       } catch (Exception e) {
           
           Logger.getLogger(PublicacionDAO.class.getName()).log(Level.SEVERE, "getClienteByTxtBusqueda", e);                   
           return null;
       }finally {
            if (em != null) {
                em.close();
            }
        }  
    }
    
    public List<Cliente> getLista(boolean activos){
        
        try{            
            String sQuery = "SELECT m FROM Cliente m "                                
                + (activos ? " WHERE m.activo = 'S'" : "" )
                +" ORDER BY m.razonSocial";                       

            return execQueryList(sQuery);

        } catch (Exception e) {

            System.err.print("ERROR: " + e.getMessage());
            return null;
        }        
    }

}
